/**
 * 
 */
package org.melord.cap;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.StringReader;

import jpcap.PacketReceiver;
import jpcap.packet.Packet;
import jpcap.packet.TCPPacket;

import org.dom4j.DocumentException;

/**
 * @author melord
 * 
 */
public class HttpMonitor implements PacketReceiver {
	private HostsManager hostManager = new HostsManager();

	public void loadHosts() throws Exception {
		try {
			hostManager.parse(Constants.HOSTS_FILE);
		} catch (DocumentException e) {
			System.out.println(e.toString());
			throw new RuntimeException("无法解析hosts.xml,请确认配置文件正确!");
		} catch (Exception e) {
			System.out.println(e.toString());
			throw e;
		}
	}

	public boolean add(String url) {
		boolean ret = false;
		// for (Site site : sites) {
		// if (site.url.equalsIgnoreCase(url)) {
		// break;
		// }
		// }
		// if (!ret) {
		// Site site = new Site();
		// site.url = url;
		// ret = sites.add(site);
		// }
		return ret;
	}

	public boolean del(String url) {
		boolean ret = false;
		// Site toDel = null;
		// for (Site site : sites) {
		// if (site.url.equalsIgnoreCase(url)) {
		// toDel = site;
		// break;
		// }
		// }
		// ret = sites.remove(toDel);
		return ret;
	}

	@Override
	public void receivePacket(Packet p) {
		if (p instanceof TCPPacket) {
			TCPPacket tcp = (TCPPacket) p;
			// System.out.println("receive tcp ");
			if (isHttp(tcp)) {
				HttpRequestParser parser = new HttpRequestParser();
				try {
					final HttpRequest request = parser
							.parser(new ByteArrayInputStream(tcp.data));
					// System.out.println(request);
					Host host = match(request);
					if (host != null) {
						if (callback != null) {
							callback.receive(host);
						}
					}
				} catch (IOException e) {

				}

			}
		}
	}

	private boolean isHttp(TCPPacket p) {
		boolean ret = false;
		// if (true || p.src_port == 80 || p.dst_port == 80) 
		{
			try {
				BufferedReader in = new BufferedReader(new StringReader(
						new String(p.data)));

				String method = in.readLine();
				if (method == null || method.indexOf("HTTP") == -1) {
					// this packet doesn't contain HTTP header
					method = "Not HTTP Header";
				} else {
					System.out.println(method);
					String l;
					// read headers
					while ((l = in.readLine()).length() > 0)
						// headers.addElement(l);
						
//						System.out.println(l);
//						System.out.println(">>>>>>>>>>>>>>>>>>>>>>>>>");
						ret = true;
				}
			} catch (IOException e) {
			}
		}
		return ret;
	}

	private Callback callback;

	public void setCallback(Callback callback) {
		this.callback = callback;
	}

	public static interface Callback {
		void receive(Host host);
	}

	public Host match(HttpRequest request) {
		Host ret = null;
		for (Host h : hostManager.getHosts()) {
			if (h.match(request.getHost()) || h.match(request.getReferer())) {
				ret = h;
				ret.setHttpRequest(request);
				break;
			}
		}
		return ret;
	}
}
